const db = require('../db')

exports.setMsgtoDB = (data, callback) => {
  // console.log(data)
  if (data.message_type === 'text') {
    setText(data, callback)
  }
}

const setText = (data, callback) => {
  const { sender_id, receiver_id, content } = data

  const setData = 'insert into easy_chat.messages set ?'
  db.query(setData, { sender_id, receiver_id, message_type: 1, content }, (err, result) => {
    if (err) {
      console.log(err)
      callback({
        code: 400,
        message: 'fali'
      })
    }
    
    callback({
      code: 200,
      message: 'success',
      data: {
        message_id: result.insertId,
        sender_id,
        receiver_id,
        message_type: 1,
        content,
        is_read: 0
      }
    })
  })
}
